﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class TradesWnd : Form
    {
        public TradesWnd()
        {
            InitializeComponent();
        }

        private void Trades_Load(object sender, EventArgs e)
        {
            try
            {
	            DataTable tradesTable = DailyTransactionsServices.GetLoanAccountCollectionDataTableByClientIdAndDate("*", DateTime.Today);
	            dataGridView.DataSource = tradesTable;
            }
            catch (System.Exception ex)
            {
            	
            }

            try
            {
                AutoCompleteStringCollection data = new AutoCompleteStringCollection();
                AutoCompleteStringCollection data2 = new AutoCompleteStringCollection();

                ClientCollection clientCollection = ClientService.GetClientCollection();

                for (int iI = 0; iI < clientCollection.Count; iI++)
                {
                    data.Add(clientCollection[iI].Code);
                    data2.Add(clientCollection[iI].BankAccNo);
                }

                textBoxClientCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                textBoxClientCode.AutoCompleteSource = AutoCompleteSource.CustomSource;
                textBoxClientCode.AutoCompleteCustomSource = data;

                textBoxAccNO.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                textBoxAccNO.AutoCompleteSource = AutoCompleteSource.CustomSource;
                textBoxAccNO.AutoCompleteCustomSource = data2;
            }
            catch (System.Exception ex)
            {
            }

            labelSelDate.Text = DateTime.Today.ToLongDateString();

            WindowState = FormWindowState.Maximized;
        }

        private void Trades_SizeChanged(object sender, EventArgs e)
        {
            dataGridView.Height = Size.Height - 140;
            dataGridView.Width = Size.Width - 40;

            labelClientCode.Left = Size.Width - 760;
            textBoxClientCode.Left = labelClientCode.Right + 7;
            labelAccNO.Left = textBoxClientCode.Right + 10;
            textBoxAccNO.Left = labelAccNO.Right + 7;
            labelTradeDate.Left = textBoxAccNO.Right + 10;
            dateTimePicker1.Left = labelTradeDate.Right + 7;
            buttonGO.Left = dateTimePicker1.Right + 10;
        }

        private void buttonGO_Click(object sender, EventArgs e)
        {
            labelSelDate.Text = dateTimePicker1.Value.ToLongDateString();
            string sClientID = "*";
            if (textBoxClientCode.Text != "")
            {
                sClientID = textBoxClientCode.Text;
            }
            else if (textBoxAccNO.Text != "")
            {
                try
                {
                    sClientID = ClientService.GetClientCodeBankAccNo(textBoxAccNO.Text);
                }
                catch (System.Exception ex)
                {
                    sClientID = "n/a";
                }
            }

            try
            {
	            DataTable tradesTable = DailyTransactionsServices.GetLoanAccountCollectionDataTableByClientIdAndDate(sClientID, dateTimePicker1.Value);
	            dataGridView.DataSource = tradesTable;
	            dataGridView.Refresh();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Database error."+ ex.Message, "Error");
            }
        }

        private void toolStripButtonAdd_Click(object sender, EventArgs e)
        {
            AddTrade oAddNewTradeDlg = new AddTrade();
            oAddNewTradeDlg.ShowDialog();
        }

        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            try
            {
                string sExeID = dataGridView.CurrentRow.Cells[3].Value.ToString();
                DateTime date = (DateTime)dataGridView.CurrentRow.Cells[1].Value;
                EditTrade editTradeDlg = new EditTrade(sExeID, date);
                editTradeDlg.ShowDialog();

	            DataTable tradesTable = DailyTransactionsServices.GetLoanAccountCollectionDataTableByClientIdAndDate("*", dateTimePicker1.Value);
	            dataGridView.DataSource = tradesTable;
	            dataGridView.Refresh();
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            DailyTransactions transaction = null;
            try
            {
                string sExeID = dataGridView.CurrentRow.Cells[3].Value.ToString();
                DateTime date = (DateTime)dataGridView.CurrentRow.Cells[1].Value;

                transaction = DailyTransactionsServices.GetDailyTransactionsByExecIdAndDate(sExeID, date);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Invalid execution ID or trade date", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (transaction == null)
            {
                MessageBox.Show("Invalid execution ID or trade date", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            DialogResult result = MessageBox.Show("Do you really want to delete the selected transaction? ", "Confirmation", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

            if (result != DialogResult.Yes)
            {
                return;
            }

            try
            {
                DailyTransactionsServices.Delete(transaction.ExecutionId, transaction.TradeDate);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to delete transaction\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            dataGridView.Rows.RemoveAt(dataGridView.SelectedRows[0].Index);
            MessageBox.Show("Transaction deleted successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void textBoxAccNO_TextChanged(object sender, EventArgs e)
        {
            if (textBoxAccNO.Text != "")
            {
                textBoxClientCode.Text = "";
            }
        }

        private void textBoxClientCode_TextChanged(object sender, EventArgs e)
        {
            if (textBoxClientCode.Text != "")
            {
                textBoxAccNO.Text = "";
            }
        }
    }
}
